<?php
/**
 * 用户模型
 *
 */
class Model_Order_Car extends Model_Base{
	
	// 用户表名
	private $userTable = '';
	private $departmentTable = '';
	private $visitorTable = '';
	private $orderCarTable = '';
	private $orderDinnerTable = '';
	private $documentTable = '';
	
	/**
	 * 构造函数
	 *
	 */
	public function __construct(){
		parent::__construct();
		$this->userTable = tabname('user');
		$this->departmentTable = tabname('department');
		$this->visitorTable =tabname('visitor');
		$this->orderCarTable = tabname('order_car');
		$this->orderDinnerTable = tabname('order_dinner');
		$this->documentTable = tabname('document');
	}
	
	public function listOrderCar($param){
		$result = array();
		$where = "is_canceled = 0";
		if(isset($param['uid']) && !empty($param['uid'])){
			$where .= " AND uid = ".$param['uid'];
		}
		if(isset($param['id']) && !empty($param['id'])){
			$where .= " AND id = ".$param['id'];
		} elseif(isset($param['date1']) && !empty($param['date1']) && isset($param['date2']) && !empty($param['date2'])){
			$where .= " AND date >= '".$param['date1']."' AND date <= '".$param['date2']."'";
		}
		$sql1 = "SELECT COUNT(id) FROM ".$this->orderCarTable." WHERE ".$where;
		$result['total'] = $this->db->fetchOne($sql1);
		
		$sql2 = "SELECT * FROM ".$this->orderCarTable." WHERE ".$where." ORDER BY id DESC";
		if(!empty($param['rows']) && ($param['rows']>0)){
			$sql2 .= " LIMIT ".$param['offset'].",".$param['rows'];
		}
		$lists = $this->db->fetchAll($sql2);
		foreach ($lists as $key=>$row){
			$lists[$key]['num'] = "TRA".str_pad(intval($row['id']), 10, "0", STR_PAD_LEFT);
		}
		$result['rows'] = $lists;
		
		$sql3 = "SELECT SUM(amount_estimated) AS sum_estimated, SUM(amount_real) AS sum_real FROM ".$this->orderCarTable." WHERE ".$where;
		$sum_arr = $this->db->fetchAll($sql3);
		foreach ($sum_arr as $key => $value){
			$sum_arr[$key]['num'] = "合计";
			$sum_arr[$key]['amount_estimated'] = $value['sum_estimated'];
			$sum_arr[$key]['amount_real'] = $value['sum_real'];
		}
		$result['footer'] = $sum_arr;
		
		return $result;
	}
	
	public function comboboxUser(){
		$sql = "SELECT id, sirname, givenname"
				." FROM ".$this->userTable
				." WHERE is_deleted =0";
	
		$lists = $this->db->fetchAll($sql);
		foreach ($lists as $key=>$row){
			$lists[$key]['text'] = $row['sirname'].$row['givenname'];
		}
		return $lists;
	}
	
	public function getUname($id){
		$sql = "SELECT sirname, givenname"
				." FROM ".$this->userTable
				." WHERE id =".$id;
	
		$row = $this->db->fetchRow($sql);
		return $row['sirname'].$row['givenname'];
	}
	
	public function addOrderCar($sets){
	$this->db->insert($this->orderCarTable,$sets);
		$ordercar_id = $this->db->lastInsertId();
		if($ordercar_id>0){
			return true;
		}
		else{
			return false;
		}
	}
	
	public function updateOrderCar($id,$sets){
		if(empty($id)){
			return false;
		}else{
			$ret = $this->db->update($this->orderCarTable,$sets,array('id'=>$id));
			if($ret){
				return true;
			}
			else{
				return false;
			}
		}
	}
	
	public function delOrderCar($id){
		if(empty($id)){
			return false;
		}else{
			$ret = $this->db->update($this->orderCarTable,array('is_canceled'=>1),array('id'=>$id));
			if($ret){
				return true;
			}
			else{
				return false;
			}
		}
	}
	
	public function sumOrderCar($date1,$date2){
		$sql = 'SELECT uid, uname, SUM(amount_estimated) AS sum_estimated, SUM(amount_real) AS sum_real, COUNT(id) AS times
				FROM '.$this->orderCarTable.'
				WHERE date >= "'.$date1.'" AND date <= "'.$date2.'"
				AND is_canceled =0
				GROUP BY uid
				ORDER BY sum_real DESC, sum_estimated DESC, times DESC';
		$sum_arr = $this->db->fetchAll($sql);
		foreach ($sum_arr as $key => $value){
			$sum_arr[$key]['date1'] = $date1;
			$sum_arr[$key]['date2'] = $date2;
		}
		return $sum_arr;
	}
	
	public function totalOrderCar($date1,$date2){
		$sql = 'SELECT SUM(amount_estimated) AS sum_estimated, SUM(amount_real) AS sum_real, COUNT(id) AS times
				FROM '.$this->orderCarTable.'
				WHERE date >= "'.$date1.'" AND date <= "'.$date2.'"
				AND is_canceled =0';
		$sum_arr = $this->db->fetchAll($sql);
		foreach ($sum_arr as $key => $value){
			$sum_arr[$key]['date1'] = "合计";
		}
		return $sum_arr;
	}
}